import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:number_spark_flutter/style/ns_style.dart';
import 'package:number_spark_flutter/widgets/brick/ns_brick.dart';

import 'record_bottom_sheet_logic.dart';

class NSRecordBottomSheetPage extends StatefulWidget {
  NSRecordBottomSheetPage({Key? key}) : super(key: key);

  @override
  State<NSRecordBottomSheetPage> createState() => _NSRecordBottomSheetPageState();
}

class _NSRecordBottomSheetPageState extends State<NSRecordBottomSheetPage> {
  final logic = Get.put(NSRecordBottomSheetLogic());

  @override
  void dispose() {
    Get.delete<NSRecordBottomSheetLogic>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        GetBuilder<NSRecordBottomSheetLogic>(builder: (logic) {
          if (logic.recordIngTime > 0) {
            return NSText(
              margin: EdgeInsets.only(bottom: 17.w),
              text: '${logic.recordIngTime}s',
              textColor: Colors.white,
              fontSize: 20.sp,
              fontFamily: 'Modak-Regular',
            );
          }
          return NSText(
            margin: EdgeInsets.only(bottom: 17.w),
            text: 'Long press to start recording.',
            textColor: Colors.white,
            fontSize: 16.sp,
            fontFamily: 'Modak-Regular',
          );
        }),
        Stack(
          alignment: Alignment.topCenter,
          children: [
            NSImage(
              assetImage: 'img_bg_record_sheet.png',
              width: double.infinity,
            ),
            NSStack(
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 65.w),
              children: [
                GetBuilder<NSRecordBottomSheetLogic>(builder: (logic) {
                  return NSImage(
                    assetImage: logic.isMoveOut ? 'icon_record_stop.png' : 'icon_record_start.png',
                    width: 80.w,
                  );
                }),
                GetBuilder<NSRecordBottomSheetLogic>(builder: (logic) {
                  return SizedBox(
                    width: 104.w,
                    height: 104.w,
                    child: CircularProgressIndicator(
                      value: logic.recordIngTime / logic.maxRecordTime,
                      valueColor: AlwaysStoppedAnimation<Color>(kAppColor('#BAF073')),
                      strokeWidth: 3.w,
                    ),
                  );
                }),
              ],
              onLongPress: () {
                logic.startRecording();
              },
              onLongPressEnd: (details) {
                logic.stopRecording();
              },
              onLongPressMoveUpdate: (e) {
                bool oldMove = logic.isMoveOut;
                print(e.localPosition.dy);
                if (e.localPosition.dy < 0) {
                  logic.isMoveOut = true;
                } else {
                  logic.isMoveOut = false;
                }
                if (oldMove != logic.isMoveOut) {
                  logic.update();
                }
              },
            )
          ],
        )
      ],
    );
  }
}
